// https://router.vuejs.org/zh/
import { RouteRecordRaw, createRouter, createWebHistory } from 'vue-router'
import { i18n } from '@/lang/index';
// 定义路由，每个路由都需要映射到一个组件
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: "home",
    meta: { index: 0, title: i18n.global.t('home') },
    component: () => import('@/views/home/index.vue'),
  },
  {
    path: '/about-us',
    name: "about-us",
    meta: { index: 0, title: i18n.global.t('menu1_1') },
    component: () => import('@/views/menu/about-us.vue'),
  },
  {
    path: '/z-plan',
    name: "z-plan",
    meta: { index: 0, title: i18n.global.t('menu1_2') },
    component: () => import('@/views/menu/z-plan.vue'),
  },
  {
    path: '/assets',
    name: "assets",
    meta: { index: 0, title: i18n.global.t('menu2_1') },
    component: () => import('@/views/menu/assets.vue'),
  },
  {
    path: '/assetsRecord',
    name: "assetsRecord",
    meta: { index: 0, title: i18n.global.t('menu2_1') },
    component: () => import('@/views/menu/assetsRecord.vue'),
  },
  {
    path: '/pledge',
    name: "pledge",
    meta: { index: 0, title: i18n.global.t('menu2_2') },
    component: () => import('@/views/menu/pledge.vue'),
  },
  {
    path: '/invitation',
    name: "invitation",
    meta: { index: 0, title: i18n.global.t('menu3_1') },
    component: () => import('@/views/menu/invitation.vue'),
  },
  {
    path: '/community',
    name: "community",
    meta: { index: 0, title: i18n.global.t('menu3_2') },
    component: () => import('@/views/menu/community.vue'),
  },
  {
    path: '/ecology',
    name: "ecology",
    meta: { index: 0, title: i18n.global.t('menu3_3') },
    component: () => import('@/views/menu/ecology.vue'),
  },
  {
    path: '/contract',
    name: "contract",
    meta: { index: 0, title: i18n.global.t('pledgeAddress') },
    component: () => import('@/views/menu/contract.vue'),
  },
  {
    path: '/redemption',
    name: "redemption",
    meta: { index: 0, title: i18n.global.t('redemptionContract') },
    component: () => import('@/views/menu/redemption.vue'),
  },
  {
    path: '/withdrawRecord',
    name: "withdrawRecord",
    meta: { index: 0, title: i18n.global.t('withdrawalsRecord') },
    component: () => import('@/views/menu/withdrawRecord.vue'),
  },
  {
    path: '/nodeApplication',
    name: "nodeApplication",
    meta: { index: 0, title: i18n.global.t('nodeApplication') },
    component: () => import('@/views/menu/nodeApplication.vue'),
  },
  { path: "/:pathMatch(.*)", redirect: "/" }
]

// 创建路由实例并传递 `routes` 配置
const router = createRouter({
  history: createWebHistory(process.env.VUE_APP_PUBLIC_PATH),
  // history: createWebHashHistory('/'),
  routes,
  scrollBehavior() {
    // 始终滚动到顶部
    return { top: 0 }
  },
})

router.beforeEach((_to: any, _from, next) => {
  document.title = _to.meta.title || 'Your Default Title'
  next()
})


// 导出路由实例，并在 `main.ts` 挂载
export default router
